<?php

namespace app\admin\model;

use app\common\model\BaseModel;


class Block extends BaseModel
{





    // 表名
    protected $name = 'block';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text'
    ];


    public function getList($where, $size = 12, $field = '*', $order = 'weigh desc')
    {
        $list = $this->where(function ($query) use ($where) {
            if (isset($where['type'])&&$where['type']){
                $query->where('type',$where['type']);
            }
            if (isset($where['status'])&&$where['status']){
                $query->where('status',$where['status']);
            }

            if (isset($where['name'])&&$where['name']){
                $query->where('name',$where['name']);
            }
        })
            ->with([])
            ->field($field)
            ->order($order)
            ->paginate($size);
        $list->each(function ($item) {
            $item->start_status =  (int)$item->start_status;
        });
        return $list->toArray();

    }

    /**
    * @param Model $row
    */
    protected static function onAfterInsert($row){
        $pk = $row->getPk();
        $row->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
    }


    public function getStatusList()
    {
        return [ '1' => __('Status 1'),'0' => __('Status 0')];
    }

    public function getUrlTypeList()
    {
        return [ 'site' => __('Site'),'miniapp' => __('Miniapp')];
    }

    public function getStartStatusList()
    {
        return [ '0' => __('StartStatus 0'),'1' => __('StartStatus 1'),'2'=>__('StartStatus 2')];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    /**
     * 读取分类类型.
     *
     * @return array
     */
    public static function getBlockTypeList()
    {
        $typeList = config('site.blocktype');
        foreach ($typeList as $k => &$v) {
            $v = __($v);
        }

        return $typeList;
    }

    public  function  getBlockTypeSet(){

        return [
            'jinggang'=>'首页金刚位',
            'vipintro'=>'首页VIP赠送',
            'zhiqing'=>'知情同意',
            'indexfocus'=>'首页顶部轮播',
            'viplevel'=>'会员等级说明',
        ];
    }


}
